package com.qfedu.web;

import com.qfedu.model.Administrator;
import com.qfedu.model.MilkTea;
import com.qfedu.model.User;
import com.qfedu.service.AdministratorService;
import com.qfedu.service.impl.AdministratorServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author han tao
 * @version 1.0
 * @date 2023/4/17 17:22
 * @project milky-tea
 */
@WebServlet("/admin")
public class AdministratorServlet extends BaseServlet{
    AdministratorService administratorService = new AdministratorServiceImpl();

    /**
     *  登录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        // 获取参数
        String aname = req.getParameter("aname");
        String password = req.getParameter("password");
        Administrator admin = new Administrator();
        admin.setAname(aname);
        admin.setPassword(password);
        Administrator administrator = administratorService.login(admin);
        if (administrator != null){
            // 登录成功
            req.getSession().setAttribute("admin",administrator);
            resp.sendRedirect("/pages/admin/index.jsp");
        }else {
            // 登录失败
            req.setAttribute("aname", aname);
            resp.setContentType("text/html;charset=utf-8");
            req.setAttribute("msg", "管理员的用户名或者密码错误");
            req.getRequestDispatcher("/pages/admin/login.jsp").forward(req,resp);
        }
    }

    /**
     * 修改管理员信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void updateAdmin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        String aname = req.getParameter("aname");
        String password = req.getParameter("password");
        Administrator admin = new Administrator();
        admin.setAname(aname);
        admin.setPassword(password);
        System.out.println(admin);
        int row = administratorService.updateAdmin(admin);
        if (row>0){
            // 修改成功
            // req.getRequestDispatcher("/admin?action=login").forward(req,resp);
            resp.sendRedirect("/admin?action=login");
        }else {
            //修改失败 跳转到错误页面
            req.setAttribute("message","修改失败，请重新操作");
            req.getRequestDispatcher("/pages/admin/error.jsp").forward(req,resp);
        }
    }

    /**
     * 添加奶茶
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void addTea(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException   {
        String tname = req.getParameter("tname");
        BigDecimal price = BigDecimal.valueOf(Integer.parseInt(req.getParameter("price")));
        String genre = req.getParameter("genre");
        String imgPath = req.getParameter("img_path");
        MilkTea tea = new MilkTea();
        tea.setTname(tname);
        tea.setPrice(price);
        tea.setGenre(genre);
        tea.setImg_path(imgPath);
        System.out.println(tea);
        int row = administratorService.addTea(tea);
        if (row > 0){
            // 添加成功
            System.out.println("成功");
            req.getRequestDispatcher("/admin?action=findTea").forward(req,resp);
        }else {
            // 添加失败 跳转到错误页面
            System.out.println("失败");
            req.setAttribute("message","添加失败，请重新操作");
            req.getRequestDispatcher("/pages/admin/tea/error.jsp").forward(req,resp);
        }
    }

    /**
     * 修改奶茶数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void updateTea(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        try {
            Map<String, String[]> parameterMap = req.getParameterMap();
            MilkTea tea = new MilkTea();
            BeanUtils.populate(tea,parameterMap);
            int row = administratorService.updateTea(tea);
            if (row>0){
                // 修改成功
                req.getRequestDispatcher("/admin?action=findTea").forward(req,resp);
            }else {
                // 修改失败 跳转到错误页面
                req.setAttribute("message","修改失败，请重新操作");
                req.getRequestDispatcher("/pages/admin/tea/error.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除奶茶
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void deleteTea(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        int tid = Integer.parseInt(req.getParameter("tid"));
        int row = administratorService.deleteTea(tid);
        if (row > 0){
            req.getRequestDispatcher("/admin?action=findTea").forward(req,resp);
        }else {
            req.setAttribute("message","删除失败，请重新操作");
            req.getRequestDispatcher("/pages/admin/tea/error.jsp").forward(req,resp);
        }
    }

    /**
     * 查询全部奶茶
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void findTea(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        List<MilkTea> list = administratorService.findAllTea();
        if (list != null){
            req.getSession().setAttribute("list",list);
            resp.sendRedirect("/pages/admin/tea/goodList.jsp");
            // req.getRequestDispatcher("/pages/admin/tea/goodList.jsp").forward(req,resp);
        }else {
            req.setAttribute("message","查询失败，请重新操作");
            req.getRequestDispatcher("/pages/admin/tea/error.jsp").forward(req,resp);
        }
    }

    /**
     * 根据ID查询奶茶
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void findTeaByTid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        int tid = Integer.parseInt(req.getParameter("tid"));
        MilkTea tea = administratorService.findTeaByTid(tid);
        if (tea != null){
            //查询成功
            req.setAttribute("milkTea",tea);
            req.getRequestDispatcher("/pages/admin/tea/updateTea.jsp").forward(req,resp);
        }else {
            //查询失败 跳转到错误页面
            req.setAttribute("message","没有该该奶茶，请重新操作");
            req.getRequestDispatcher("/pages/admin/tea/error.jsp").forward(req,resp);
        }
    }

    /**
     * 查询所有用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void findAllUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        List<User> list = administratorService.findAllUser();
        if (list != null){
            req.setAttribute("users",list);
            req.getRequestDispatcher("/pages/admin/user/userList.jsp").forward(req,resp);
        }else {
            req.setAttribute("message","查询失败，请重新操作");
            req.getRequestDispatcher("/pages/admin/error.jsp").forward(req,resp);
        }
    }

    /**
     * 根据ID查询用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void findUserById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        //获取参数
        int id = Integer.parseInt(req.getParameter("id"));
        User user = administratorService.findUserById(id);
        if (user != null){
            //查询成功
            req.setAttribute("users",user);
            req.getRequestDispatcher("/pages/admin/user/update.jsp").forward(req,resp);
        }else {
            //查询失败 跳转到错误页面
            req.setAttribute("message","没有该用户，请重新操作");
            req.getRequestDispatcher("/pages/admin/user/error.jsp").forward(req,resp);
        }
    }

    /**
     * 根据用户名查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void findUserByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        String username = req.getParameter("username");
        System.out.println(username);
        User user = administratorService.findUserByName(username);
        if (user != null){
            //查询成功
            req.setAttribute("users",user);
            req.getRequestDispatcher("/pages/admin/user/user.jsp").forward(req,resp);
            // resp.sendRedirect("/pages/admin/user/user.jsp");
        }else {
            //查询失败 跳转到错误页面
            req.setAttribute("message","没有该用户，请重新操作");
            req.getRequestDispatcher("/pages/admin/user/error.jsp").forward(req,resp);
        }
    }

    /**
     * 删除用户
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        //获取参数
        int id = Integer.parseInt(req.getParameter("id"));
        int row = administratorService.deleteById(id);
        if (row > 0){
            //修改成功
            req.getRequestDispatcher("/admin?action=findAllUser").forward(req,resp);
        }else {
            //修改失败 跳转到错误页面
            req.setAttribute("message","删除失败，请重新操作");
            req.getRequestDispatcher("/pages/admin/user/error.jsp").forward(req,resp);
        }

    }

    /**
     * 修改用户信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void updateUserById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
        try {
            Map<String, String[]> parameterMap = req.getParameterMap();
            User user = new User();
            BeanUtils.populate(user,parameterMap);
            int row = administratorService.updateUserById(user);
            if (row>0){
                // 修改成功
                req.getRequestDispatcher("/admin?action=findAllUser").forward(req,resp);
            }else {
                //修改失败 跳转到错误页面
                req.setAttribute("message","修改失败，请重新操作");
                req.getRequestDispatcher("/pages/admin/user/error.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 退出
     */
    public void exit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //销毁session  重新进入到登录页面
        req.getSession().invalidate();
        req.getRequestDispatcher("/pages/admin/login.jsp").forward(req,resp);
    }
}